<template>
	<view>
		<view v-if="oilCard.length <= 0">
			<view class="title">请添加加油卡</view>
		</view>
		<view v-else v-for="(v, index) in oilCard" :key="index" class="card">
			<view class="card_top">
				<view class="card_top_left">{{ v.cardNumber }}</view>
				<view class="card_top_right">余额{{ v.amount }}￥</view>
			</view>
			<view class="card_bottom">
				<view class="card_bottom_left">{{ v.storeName }}</view>
				<input class="card_bottom_right" type="password" v-model="v.cardPassword" />
			</view>
			<view class="buttons">
				<view>充值</view>
				<view>修改</view>
				<view @click="reportLossOrNot(v, 'loss')">挂失</view>
				<view @click="reportLossOrNot(v, 'logout')">注销</view>
			</view>
		</view>
		<button type="primary" @click="addCard()" style="margin-top: 20rpx">绑定油卡</button>
		<view class="recharge">
			<view>
				<input type="text" placeholder="请输入加油卡号码" v-model="currentFuelCard" />
				<image src="../../static/修改.png" @click="openPicker"></image>
			</view>
			<view>充油费</view>

			<view class="amount1">
				<view @click="fuelCardAmountSelection(1)" :class="[state == 1 ? 'active' : '']">{{ numList[0] }}元</view>
				<view @click="fuelCardAmountSelection(2)" :class="[state == 2 ? 'active' : '']">{{ numList[1] }}元</view>
				<view @click="fuelCardAmountSelection(3)" :class="[state == 3 ? 'active' : '']">{{ numList[2] }}元</view>
			</view>
			<view class="amount1">
				<view @click="fuelCardAmountSelection(4)" :class="[state == 4 ? 'active' : '']">{{ numList[3] }}元</view>
				<view @click="fuelCardAmountSelection(5)" :class="[state == 5 ? 'active' : '']">{{ numList[4] }}元</view>
				<view @click="fuelCardRecharge">充值</view>
			</view>
		</view>
		<view>
			<uv-picker ref="picker" :columns="cardNumList" @confirm="confirm"></uv-picker>
		</view>
	</view>
</template>

<script>
import http from '@/util/http.js';
export default {
	data() {
		return {
			userId: 1,
			oilCard: [],
			state: 0,
			cardNumList: [],
			currentFuelCard: null,
			numList: [50, 100, 200, 500, 1000],
			amount: 0
		};
	},
	onLoad() {
		this.userId = uni.getStorageSync('uid');
		this.getUserOilCard();
	},
	onShow() {
		this.state = 0;
		this.amount = 0;
	},
	methods: {
		// 选择用户的油卡
		openPicker() {
			this.$refs.picker.open();
		},
		confirm(e) {
			this.currentFuelCard = e.value[0];
		},

		//用户选择金额
		fuelCardAmountSelection(e) {
			if (this.state == e) {
				this.state = 0;
				this.amount = 0;
			} else {
				this.state = e;
				this.amount = this.numList[e - 1];
			}
		},

		// 油卡充值
		fuelCardRecharge() {
			if (this.amount == 0 && this.currentFuelCard != null) {
				uni.showToast({
					title: '请选择充值金额',
					icon: 'error'
				});
			} else if (this.currentFuelCard == null && this.amount != 0) {
				uni.showToast({
					title: '请选择卡号',
					icon: 'error'
				});
			} else if (this.currentFuelCard == null && this.amount == 0) {
				uni.showToast({
					title: '请选择卡和金额',
					icon: 'error'
				});
			} else {
				http({
					url: '/userOil/rechargeCard',
					data: {
						userId: this.userId,
						cardNumber: this.currentFuelCard,
						amount: this.amount
					},
					success: (res) => {
						if (res.data > 0) {
							uni.showToast({
								title: '充值成功',
								image: '../../static/成功提示.png'
							});
							this.getUserOilCard();
						} else {
							uni.showToast({
								title: '请输入正确的卡号',
								icon: 'error'
							});
						}
					}
				});
			}
		},

		//用户添加油卡并绑定
		addCard() {
			uni.showModal({
				editable: true,
				title: '油卡添加',
				content: this.cardNumber,
				placeholderText: '请输入你的油卡号',
				showCancel: true,
				cancelText: '取消',
				confirmText: '确定',
				success: (res) => {
					if (res.confirm) {
						http({
							url: '/userOil/updateCard',
							data: {
								userId: this.userId,
								cardStatus: 'a',
								cardNumber: res.content
							},
							success: (res) => {
								if (res.data > 0) {
									uni.showToast({
										title: '添加成功',
										image: '../../static/成功提示.png'
									});
									this.getUserOilCard();
								} else {
									uni.showToast({
										title: '请输入正确的卡号',
										icon: 'error'
									});
								}
							}
						});
					} else if (res.cancel) {
					}
				}
			});
		},

		// 确定是否挂失
		reportLossOrNot(v, e) {
			if (e == 'loss') {
				let that = this;
				uni.showModal({
					editable: false,
					content: '确认挂失这个油卡吗？',
					title: '提示',
					cancelText: '取消',
					confirmText: '确定',
					success: function (res) {
						if (res.confirm) {
							that.updataCardState(v, e);
						} else if (res.cancel) {
						}
					}
				});
			} else {
				let that = this;
				uni.showModal({
					editable: false,
					content: '确认挂失这个油卡吗？',
					title: '提示',
					cancelText: '取消',
					confirmText: '确定',
					success: function (res) {
						if (res.confirm) {
							uni.showModal({
								editable: false,
								content: '请再次确认挂失这个油卡吗？',
								title: '提示',
								cancelText: '取消',
								confirmText: '确定',
								success: function (res) {
									if (res.confirm) {
										that.updataCardState(v, e);
									} else if (res.cancel) {
									}
								}
							});
						} else if (res.cancel) {
						}
					}
				});
			}
		},

		// 挂失或者注销油卡
		updataCardState(v, e) {
			http({
				url: '/userOil/updateCard',
				data: {
					userId: this.userId,
					cardStatus: e,
					cardNumber: v.cardNumber
				},
				success: (res) => {
					this.getUserOilCard();
					if (res.data > 0) {
						uni.showToast({
							title: '成功',
							image: '../../static/pro-success.png'
						});
						this.getUserOilCard();
					} else {
						uni.showToast({
							title: '失败',
							icon: 'error'
						});
					}
				}
			});
		},

		// 获取用户已经绑定的油卡
		getUserOilCard() {
			http({
				url: '/userOil/getUserCard',
				data: {
					userId: this.userId
				},
				success: (res) => {
					this.oilCard = res.data.data;
					let arr = [];
					this.oilCard.forEach((item, index) => {
						arr.push(item.cardNumber);
					});
					this.cardNumList[0] = arr;
				}
			});
		}
	}
};
</script>

<style>
body {
	background-color: #f0eff4;
}

/* 充值区 */
.recharge {
	margin-top: 20rpx;
	background-color: #ffffff;
	width: 100%;
}
.recharge > view:nth-child(1) {
	display: flex;
	margin-left: 20rpx;
	margin-left: 5%;
	margin-right: 5%;
}

.recharge view:nth-child(1) input {
	width: 100%;
	margin-top: 15rpx;
	margin-bottom: 15rpx;
	margin-left: 10rpx;
	font-weight: 900;
	line-height: 50rpx;
	height: 50rpx;
}
.recharge view:nth-child(1) image {
	text-align: right;
	margin-top: 15rpx;
	margin-bottom: 15rpx;
	width: 50rpx;
	height: 50rpx;
}
.recharge > view:nth-child(2) {
	font-weight: bold;
	margin-top: 15 rpx;
	margin-left: 2%;
	margin-right: 5%;
}
.amount1 {
	display: flex;
	margin-top: 10rpx;
}
.amount1 view {
	margin-left: 30rpx;
	margin-bottom: 20rpx;
	text-align: center;
	line-height: 100rpx;
	width: 210rpx;
	height: 100rpx;
	font-weight: 900;
	background-color: rgb(247, 247, 247);
	border-radius: 20rpx;
}
.amount1 view.active {
	border: 5rpx solid royalblue;
}
/* 按钮区 */
.buttons {
	display: flex;
	width: 100%;
}
.buttons view {
	text-align: center;
	width: 100rpx;
	height: 40rpx;
	line-height: 40rpx;
	font-size: 15px;
	border: 3rpx solid #000;
	border-radius: 15rpx;
	margin-bottom: 20rpx;
	margin-left: 10rpx;
}
.buttons :nth-child(1) {
	background-color: green;
	color: white;
}
.buttons :nth-child(2) {
	background-color: green;
	color: white;
}
.buttons :nth-child(3) {
	background-color: red;
	color: white;
}
.buttons :nth-child(4) {
	background-color: red;
	color: white;
}
/* 油卡详细区 */
.card {
	background-color: #ffffff;
	margin: 15rpx;
}
.card_top {
	display: flex;
	width: 100%;
}

.card_top_left {
	margin-top: 15rpx;
	margin-left: 5%;
	font-weight: bold;
	font-size: 15px;
}
.card_top_right {
	width: 100%;
	margin-top: 15rpx;
	margin-right: 3%;
	text-align: right;
}

.card_bottom {
	margin-top: 10rpx;
	display: flex;
	width: 100%;
}
.card_bottom_left {
	margin-left: 5%;
	width: 70%;
}
.card_bottom_right {
	width: 30%;
	margin-top: 15rpx;
	margin-right: 3%;
	text-align: right;
}
</style>
